package com.fgs.common.adapter;

import android.util.SparseBooleanArray;
import android.view.View;

import androidx.annotation.NonNull;

import com.fgs.common.R;
import com.fgs.common.interf.ISelectItemValue;
import com.fgs.common.utils.ToastUtil;
import com.xuexiang.xui.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2020/6/20
 * @description
 **/
public class CityAdapter<T> extends BaseRecyclerAdapter<T> {

    private boolean mIsMultiSelectMode;
    private boolean mCancelable;
    //是否显示查看更多
    private boolean mIsShowMore;

    private boolean isOpen;
    private boolean isHide;
    private int maxSelectNum = 0;

    /**
     * 最多选择几项
     *
     * @param maxSelectNum
     */
    public void setMaxSelectNum(int maxSelectNum) {
        this.maxSelectNum = maxSelectNum;
    }

    private SparseBooleanArray mSparseArray = new SparseBooleanArray();


    /**
     * 是否多选
     *
     * @param isMultiSelectMode
     * @return
     */
    public CityAdapter setIsMultiSelectMode(boolean isMultiSelectMode) {
        mIsMultiSelectMode = isMultiSelectMode;
        return this;
    }

    /**
     * 单选时选中的是否可以取消
     *
     * @param cancelable
     * @return
     */
    public CityAdapter setCancelable(boolean cancelable) {
        mCancelable = cancelable;
        return this;
    }

    /**
     * 是否显示查看更多
     *
     * @param isShowMore
     * @return
     */
    public CityAdapter setIsShowMore(boolean isShowMore) {
        mIsShowMore = isShowMore;
        return this;
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.item_city;
    }

    @Override
    protected void bindData(@NonNull RecyclerViewHolder holder, int position, T item) {
        holder.text(R.id.item_city_textView, getContentText(item));

        if (!mIsShowMore) {

            if (mIsMultiSelectMode) {
                holder.select(R.id.item_city_textView, mSparseArray.get(position));
            } else {
                holder.select(R.id.item_city_textView, getSelectPosition() == position);
            }

        } else {

            if (mIsMultiSelectMode) {
                if (position == getData().size() - 1) {

                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (position == getData().size() - 1) {
                                if (isOpen) {
                                    if (null != hideOrShowCallBack) {
                                        hideOrShowCallBack.hide();
                                    }
                                    return;
                                }
                                if (isHide) {
                                    if (null != hideOrShowCallBack) {

                                        hideOrShowCallBack.open();
                                    }
                                    return;
                                }
                            }
                        }
                    });


                } else {
                    holder.select(R.id.item_city_textView, mSparseArray.get(position));
                }

            }

        }


    }

    /**
     * 条目显示内容
     *
     * @param item
     * @return
     */
    private String getContentText(Object item) {
        if (item == null) {
            return "";
        } else if (item instanceof ISelectItemValue) {
            return ((ISelectItemValue) item).getSelectItemText();
        }
        return item.toString();
    }

    /**
     * 条目显示内容对应的值
     *
     * @param item
     * @return
     */
    private String getContentTextValue(Object item) {
        if (item == null) {
            return "";
        } else if (item instanceof ISelectItemValue) {
            return ((ISelectItemValue) item).getSelectItemTextValue();
        }
        return item.toString();
    }

    /**
     * 选择
     *
     * @param position
     * @return
     */
    public boolean select(int position) {
        return mIsMultiSelectMode ? multiSelect(position) : singleSelect(position);
    }

    /**
     * 多选
     *
     * @param positions
     */
    public void multiSelect(int... positions) {
        if (!mIsMultiSelectMode) {
            return;
        }
        for (int position : positions) {
            multiSelect(position);
        }
    }

    /**
     * 多选
     *
     * @param position
     */
    public boolean multiSelect(int position) {
        if (!mIsMultiSelectMode) {
            return false;
        }

        mSparseArray.append(position, !mSparseArray.get(position));
        notifyItemChanged(position);

        if (maxSelectNum > 0 && mSparseArray.size() > 0) {
            int size = getItemCount();
            int count = 0;
            for (int i = 0; i < size; i++) {
                if (mSparseArray.get(i)) {
                    count++;
                }
            }
            if (count > maxSelectNum) {
                String tip = "最多选" + maxSelectNum + "项";
                ToastUtil.showShortToast(tip);
                mSparseArray.append(position, !mSparseArray.get(position));
                notifyItemChanged(position);
                return true;
            }


        }


        return true;
    }

    /**
     * 单选
     *
     * @param position
     */
    public boolean singleSelect(int position) {
        return singleSelect(position, mCancelable);
    }

    /**
     * 单选
     *
     * @param position
     * @param cancelable
     */
    public boolean singleSelect(int position, boolean cancelable) {
        if (position == getSelectPosition()) {
            if (cancelable) {
                setSelectPosition(-1);
                return true;
            }
        } else {
            setSelectPosition(position);
            return true;
        }
        return false;
    }


    /**
     * @return 获取选中的内容
     */
    public T getSelectContent() {
        T value = getSelectItem();
        return value;
    }

    /**
     * @return 获取选中的内容
     */
    public String getSelectContentValue() {
        String value = getContentTextValue(getSelectItem());
        return value;
    }

    /**
     * @return 获取选中的内容
     */
    public String getSelectContentString() {
        String value = getContentText(getSelectItem());
        return value;
    }

    /**
     * @return 获取多选的内容
     */
    public List<String> getMultiContent() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < getItemCount(); i++) {
            if (mSparseArray.get(i)) {
                list.add(getContentText(getItem(i)));
            }
        }
        return list;
    }

    /**
     * @return 获取多选的内容
     */
    public List<String> getMultiContentValue() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < getItemCount(); i++) {
            if (mSparseArray.get(i)) {
                list.add(getContentTextValue(getItem(i)));
            }
        }
        return list;
    }

    /**
     * @return 获取多选的内容
     */
    public List<T> getMultiContentInfo() {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < getItemCount(); i++) {
            if (mSparseArray.get(i)) {
                list.add(getItem(i));
            }
        }
        return list;
    }

    //隐藏
    public void setHideList(List<T> newList) {
        clearData();
        getData().addAll(newList);
        notifyDataSetChanged();
        this.isHide = true;
        this.isOpen = false;
    }

    //展开
    public void setOpenList(List<T> openList) {
        clearData();
        getData().addAll(openList);
        this.isOpen = true;
        this.isHide = false;
        notifyDataSetChanged();
    }

    //不需要隐藏/展开
    public void setRealList(List<T> realList) {
        clearData();
        getData().addAll(realList);
        notifyDataSetChanged();
        this.isHide = false;
        this.isOpen = false;
    }

    //清除数据
    private void clearData() {
        getData().clear();
    }


    public void multiSelectReset() {
        mSparseArray.clear();
        notifyDataSetChanged();
    }


    private HideOrShowCallBack hideOrShowCallBack;

    public void setHideOrShowCallBack(HideOrShowCallBack hideOrShowCallBack) {
        this.hideOrShowCallBack = hideOrShowCallBack;
    }


    public interface HideOrShowCallBack {
        void hide();

        void open();
    }
}
